package com.excel.qlexpress;


import com.corpgovernment.organization.enumm.ServiceErrorEnum;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;


import java.util.List;
import java.util.Map;

import java.util.function.Function;
import java.util.stream.Collectors;


@Slf4j
public class OrgQlFunction {
    // 默认成本中心不为空时，成本中心必须也不为空，且包含成本中心
    public static ServiceErrorEnum checkOrgCost(String costCenterInfoDefault,String costCenterInfo){
        if(StringUtils.isNotBlank(costCenterInfoDefault)) {
            if(! costCenterInfoDefault.contains("/")) {
                return ServiceErrorEnum.SyncHumanResources_SyncOrgInfo_Cost_Center_Info_Default_ERROR;
            }
            if( costCenterInfoDefault.split("/").length != 2) {
                return ServiceErrorEnum.SyncHumanResources_SyncOrgInfo_Cost_Center_Info_Default_More;
            }
            if(StringUtils.isBlank(costCenterInfo)) {
                return ServiceErrorEnum.SyncHumanResources_SyncOrgInfo_Cost_Center_Info_Default_Failure;
            }
            Boolean flag = true;
            String[] costCenters = costCenterInfo.split(";");
            for (String costCenter : costCenters) {
                if(StringUtils.equals( costCenterInfoDefault, costCenter, false)) {
                    flag = false;
                }
            }
            if(flag) {
                return ServiceErrorEnum.SyncHumanResources_SyncOrgInfo_Cost_Center_Info_Default_Failure;
            }
        }
        return ServiceErrorEnum.Success;
    }
    
    

    //校验 excel重复数据 动态字段
    public static <T> List<T> getDuplicateOrgField(List<T> list, Function<T, String> fieldMapper){
        Map<String, Long> countMap = list.stream().collect(Collectors.groupingBy(fieldMapper, Collectors.counting()));
        List<T> duplicates = list.stream()
                .filter(e -> countMap.get(fieldMapper.apply(e))>1)
                .collect(Collectors.toList());
        return duplicates;
    }


}